package org.jbfilter.test.junit;

import java.util.Date;

import junit.framework.TestCase;
import junit.textui.TestRunner;

import org.jbfilter.bean.PropertyAccessor;
import org.jbfilter.bean.factory.FilterComponentBeans;
import org.jbfilter.bean.fcomps.single.ComparableFilterComponentBean;
import org.jbfilter.bean.fcomps.single.ContainsStringFilterComponentBean;
import org.jbfilter.core.JbfilterComparatorKeyword;
import org.jbfilter.test.beans.Composer;
import org.jbfilter.test.beans.factory.ComposerFactory;

public class FilterComponentsNullTesting extends TestCase {
	private Composer brahms;
	private Composer beethoven;
	private Composer andersson;
	private ContainsStringFilterComponentBean<Composer> containsFc;
	private ComparableFilterComponentBean<Composer, Date> comparableFc;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		containsFc = FilterComponentBeans.newContainsStringFilterComponent("id", new PropertyAccessor<Composer, String>() {
			public String getPropertyValue(Composer composer) {
				return composer.getParticle();
			}
		});
		comparableFc = FilterComponentBeans.newComparableFilterComponent("id", new PropertyAccessor<Composer, Date>() {
			public Date getPropertyValue(Composer composer) {
				return composer.getDied();
			}
		});
		
		brahms = ComposerFactory.getInstance().getBrahms();
		beethoven = ComposerFactory.getInstance().getBeethoven();
		andersson = ComposerFactory.getInstance().getAndersson();
	}
	
	public void testAll() {
		assertTrue(containsFc.getValue() == null);
		assertFalse(containsFc.isNullTestingEnabled());
		assertTrue(containsFc.pass(brahms));
		assertTrue(containsFc.pass(beethoven));
		containsFc.setNullTestingEnabled(true);
		assertTrue(containsFc.pass(brahms));
		assertFalse(containsFc.pass(beethoven));
		
		comparableFc.setComparatorKeyword(JbfilterComparatorKeyword.gt);
		comparableFc.setValue(null);
		assertTrue(comparableFc.pass(andersson));
		assertTrue(comparableFc.pass(beethoven));
		comparableFc.setComparatorKeyword(JbfilterComparatorKeyword.eq);
		assertTrue(comparableFc.pass(andersson));
		assertTrue(comparableFc.pass(beethoven));
		comparableFc.setNullTestingEnabled(true);
		assertTrue(comparableFc.isNullTestingEnabled());
		assertTrue(comparableFc.pass(andersson));
		assertFalse(comparableFc.pass(beethoven));
		comparableFc.setComparatorKeyword(JbfilterComparatorKeyword.le);
		assertTrue(comparableFc.pass(andersson));
		assertFalse(comparableFc.pass(beethoven));
	}
	public static void main(String[] args) {
		TestRunner.run(FilterComponentsNullTesting.class);
	}

}
